/*
 * $Log: mseq.c,v $
 * Revision 1.1  2000/05/03 14:30:04  bjc97r
 * Initial revision
 *
 */ 

#include "mseq.h"
#include <stdlib.h>


/*
 * It create an m-sequence generator with the given generator
 * polynomial and the initial register sate.
 * The pointer to the m-sequence generator is returned.
 * The max degree of the generator poly is 31.
 */
Mseq * mseq_create( unsigned deg, unsigned long poly, unsigned long seed )
{
  Mseq *mseq;

  if ( deg > 31 ) {
    return 0;
  }

  mseq = (Mseq*) malloc( sizeof(Mseq) );

  mseq->state = seed;
  mseq->mask  = poly >> 1;

  return mseq;
}

/* mseq_free() deallocates the memory space for the m-sequence generator. */
void mseq_free( Mseq * mseq )
{
  free( (void*) mseq );
}

/*
 * mseq() gives the next m-sequence bit of the m-sequence generator.
 * The generated bit is unipolar, ie 1 or 0.
 */
char mseq( Mseq * id )
{
  char seq;
  
  seq = (char)(id->state & 1);
  id->state >>= 1;
  if ( seq ) id->state ^= id->mask;

  return seq;
}


